package com.sailfish.template.infra.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充
 *
 * @author XIAXINYU3
 * @date 2021.11.5
 */
@Component
public class DatabaseTableMetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Long userId = 1L;

        this.setInsertFieldValByName("createdBy", userId, metaObject);
        this.setInsertFieldValByName("creationDate", new Date(), metaObject);
        this.setUpdateFieldValByName("lastUpdatedBy", userId, metaObject);
        this.setUpdateFieldValByName("lastUpdateDate", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userId = 1L;
        this.setUpdateFieldValByName("lastUpdatedBy", userId, metaObject);
        this.setUpdateFieldValByName("lastUpdateDate", new Date(), metaObject);
    }
}
